package br.ucb.cm.config.page.save.ui;

import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
import javax.swing.JFileChooser;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;

import br.ucb.cm.main.common.CMFileChooser;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;

public class SavePageUI extends JPanel{
	private static final long serialVersionUID = 1L;
	private IfcSavePage controller;
	private JTextField pathField;
	private CMFileChooser fileChooser;
	
	public SavePageUI(IfcSavePage controller) {
		this.controller = controller;
		this.fileChooser = new CMFileChooser("Salvar");
		initComponents();
	}
	
	private void initComponents(){
		setBorder(new TitledBorder(null, "Local para cria\u00E7\u00E3o do arquivo", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		
		JLabel lblInformeOLocal = new JLabel("Informe o local que deseja salvar a configura\u00E7\u00E3o:");
		
		pathField = new JTextField();
		pathField.setColumns(10);
		
		JButton btnNewButton = new JButton("Escolher...");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int option = getFileChooser().showSaveDialog(null);
				
				if(option == JFileChooser.APPROVE_OPTION && getFileChooser().getSelectedFile() != null){
					String path = getFileChooser().getSelectedFile().getAbsolutePath().concat(".cm");
					pathField.setText(path);
				}
			}
		});
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addComponent(lblInformeOLocal, GroupLayout.DEFAULT_SIZE, 438, Short.MAX_VALUE)
				.addGroup(groupLayout.createSequentialGroup()
					.addComponent(pathField, GroupLayout.DEFAULT_SIZE, 335, Short.MAX_VALUE)
					.addGap(10)
					.addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 89, GroupLayout.PREFERRED_SIZE)
					.addGap(4))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(10)
					.addComponent(lblInformeOLocal)
					.addGap(13)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(1)
							.addComponent(pathField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addComponent(btnNewButton)))
		);
		setLayout(groupLayout);
	}

	public CMFileChooser getFileChooser() {
		return fileChooser;
	}

	public void setFileChooser(CMFileChooser fileChooser) {
		this.fileChooser = fileChooser;
	}

	public IfcSavePage getController() {
		return controller;
	}

	public void setController(IfcSavePage controller) {
		this.controller = controller;
	}
	
	public String getFilePath(){
		return pathField.getText();
	}
	
}